import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import { Response } from '@angular/http';
import {AddressService} from "../../service/address.service";

interface IDetailAddress {
  provinces: string[];
  allCities: {name: string[]};
  allCountys: {name: string[]};
  citys: string[];
  countys: string[];
}

interface ISelectedAddress {
  selectedProvince: string;
  selectedCity: string;
  selectedCounty: string;
  provinceWasSelected: boolean;
  cityWasSelected: boolean;
  countyWasSelected: boolean;
  allWasSelected: boolean;
}
@Component({
  selector: 'app-address',
  templateUrl: './address.component.html',
  styleUrls: ['./address.component.less']
})
export class AddressComponent implements OnInit {
  @Output() addrSelected = new EventEmitter<ISelectedAddress>();
  address: IDetailAddress = {
    provinces: [],
    allCities: {name: []},
    allCountys: {name: []},
    citys: [],
    countys: [],
  };
  selectedAddress: ISelectedAddress = {
    selectedProvince: '',
    selectedCity: '',
    selectedCounty: '',
    provinceWasSelected: false,
    cityWasSelected: false,
    countyWasSelected: false,
    allWasSelected: false
  }
  constructor(private addressService: AddressService) {
    const detailAddress = localStorage.getItem('address');
    if (detailAddress) {
      this.selectedAddress = JSON.parse(detailAddress);
      this.addrSelected.emit(this.selectedAddress);
    }
  }

  ngOnInit() {
    this.addressService.getAddress().subscribe(
      (response: Response) => {
        const data = response.json();
        for (let d of data) {
          this.address.provinces.push(d.name);
           this.address.allCities[d.name] = d.city;
           for (let county of d.city) {
             this.address.allCountys[county.name] = county.area;
           }
        }
      }
    );
  }
  onSelectedProvince(pro: string) {
    this.selectedAddress.selectedProvince = pro;
    this.address.citys = this.address.allCities[pro];
    this.selectedAddress.provinceWasSelected = true;
    this.selectedAddress.cityWasSelected = false;
    this.selectedAddress.countyWasSelected = true;
  }
  onSelectedCity(c: string) {
    this.selectedAddress.selectedCity = c;
    this.address.countys = this.address.allCountys[c];
    this.selectedAddress.cityWasSelected = true;
    this.selectedAddress.countyWasSelected = false;
  }
  onSelectedCounty(county: string) {
    this.selectedAddress.selectedCounty = county;
    this.selectedAddress.countyWasSelected = false;
    this.selectedAddress.allWasSelected = true;
    this.addrSelected.emit(this.selectedAddress);
    localStorage.setItem('address', JSON.stringify(this.selectedAddress));
  }
  onSelectedTabPro () {
  this.selectedAddress.provinceWasSelected = false;
  this.selectedAddress.cityWasSelected = true;
  this.selectedAddress.countyWasSelected = true;
}
  onSelectedTabCity () {
    this.selectedAddress.provinceWasSelected = true;
    this.selectedAddress.cityWasSelected = false;
    this.selectedAddress.countyWasSelected = true;
  }
  onSelectedTabCounty() {
    this.selectedAddress.provinceWasSelected = true;
    this.selectedAddress.cityWasSelected = true;
    this.selectedAddress.countyWasSelected = false;
  }
}
